package com.cpcphone.abtestcenter.sdk;

import android.content.Context;
import android.util.Log;

import com.cpcphone.abtestcenter.AbtestCenterService;
import com.cs.bd.utils.LogUtils;
import com.cs.statistic.utiltool.Machine;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.Iterator;
import java.util.Locale;

/**
 * Created by Administrator on 2018/2/13.
 */

public class OnlineConfigAgent implements Runnable {

    public static final String URL = "http://zxl-public.oss-cn-shenzhen.aliyuncs.com/config/";

    private int cdays;

    private String cachePath;

    private Context mContext;

    private static OnlineConfigAgent sInstance;

    private AbtestCenterService.ResultCallback mResultCallback;

    private OnlineConfigAgent(Context context) {
        mContext = context;
        cachePath = new File(context.getCacheDir(), "config_" + Machine.getCurrProcessName(context)).getPath();
    }

    public static OnlineConfigAgent getInstance(Context context) {
        if (null == sInstance) {
            sInstance = new OnlineConfigAgent(context);
        }
        return sInstance;
    }

    public void start(int cdays, AbtestCenterService.ResultCallback resultCallback) {
        this.cdays = cdays;
        mResultCallback = resultCallback;
        new Thread(this).start();
        LogUtils.d("OnlineConfigAgent start");
    }

    @Override
    public void run() {
        String result;
        File file = new File(cachePath);
        if (file.exists() && (System.currentTimeMillis() - file.lastModified()) < 24 * 60 * 60 * 1000) {
            result = FileUtil.readFileToString(cachePath);
            Log.d("LogUtils", "file : " + file.lastModified());
        } else {
            byte[] bs = HttpUtils.doGet(URL + mContext.getPackageName());
            if (null != bs && bs.length > 0) {
                FileUtil.saveByteToSDFile(bs, cachePath);
                result = new String(bs);
            } else {
                return;
            }
        }
        try {
            LogUtils.d("OnlineConfigAgent run : " + result);
            JSONObject tmp = new JSONObject();
            JSONObject jsonObject = new JSONObject(result);
            JSONObject dataObject = jsonObject.optJSONObject("datas");
            Iterator<String> iterator = dataObject.keys();
            while (iterator.hasNext()) {
                String key = iterator.next();
                JSONObject tmpJsonObject = dataObject.optJSONObject(key);
                int cdays = tmpJsonObject.optInt("cdays", -1);
                if (cdays == -1 || this.cdays <= cdays) {
                    tmp.put(key, tmpJsonObject);
                }
            }
            jsonObject.put("datas", tmp);
            result = jsonObject.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != mResultCallback) {
            mResultCallback.onResponse(result);
        }

    }

    public String getResult() {
        return FileUtil.readFileToString(cachePath);
    }

}
